﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace LED_Editor.Model
{

    public class DeletingItemEventArgs : CancelEventArgs
    {
        // Fields
        private object _itemToDelete;

        // Properties
        public object ItemToDelete
        {
            get
            {
                return this._itemToDelete;
            }
            set
            {
                this._itemToDelete = value;
            }
        }
    }
    public class SavingItemEventArgs : EventArgs
    {
        // Fields
        private object _item;

        // Properties
        public object Item
        {
            get
            {
                return this._item;
            }
            set
            {
                this._item = value;
            }
        }
    }






    public class BindingSourceEx : BindingSource
    {
        public ListChangedEventHandler ListChangedEx;

        public event DeletingItemHandler DeletingItem;

        public event SavingItemHandler SavingItem;

        // Methods
        public BindingSourceEx()
        {
        }

        public BindingSourceEx(IContainer container)
        {
        }

        public override int Add(object value)
        {
            //int result = -1;
            //try
            //{
            //    this.OnSavingItem(value);
            //    result = base.Add(value);
            //}
            //catch (Exception ex)
            //{
            //    this.OnDataError(new BindingManagerDataErrorEventArgs(ex));
            //}
            //return result;
            return base.Add(value);
        }

        public override object AddNew()
        {
            return base.AddNew();
        }

        protected override void OnAddingNew(AddingNewEventArgs e)
        {
            base.OnAddingNew(e);
        }

        protected virtual void OnDeletingItem(object itemToDelete, out bool cancel)
        {
            cancel = false;
            DeletingItemEventArgs args = new DeletingItemEventArgs();
            args.ItemToDelete = itemToDelete;
            if (DeletingItem != null)
            {
                DeletingItem(this, args);
            }
            cancel = args.Cancel;
        }

        protected override void OnListChanged(ListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemChanged)
            {
                this.OnSavingItem(base.List[e.NewIndex]);
            }
            //else
           // {
                base.OnListChanged(e);
           // }
        }

        protected virtual void OnSavingItem(object item)
        {
            SavingItemEventArgs args = new SavingItemEventArgs();
            args.Item = item;
            if (SavingItem != null)
            {
                SavingItem(this, args);
            }
        }

        public override void Remove(object value)
        {
            try
            {
                bool cancel;
                this.OnDeletingItem(value, out cancel);
                if (!cancel)
                {
                    base.Remove(value);
                }
            }
            catch (Exception ex)
            {
                this.OnDataError(new BindingManagerDataErrorEventArgs(ex));
            }
        }

        public override void RemoveAt(int index)
        {
            try
            {
                bool cancel;
                this.OnDeletingItem(base.List[index], out cancel);
                if (!cancel)
                {
                    base.RemoveAt(index);
                }
            }
            catch (Exception ex)
            {
                this.OnDataError(new BindingManagerDataErrorEventArgs(ex));
            }
        }

        // Properties
        public override bool SupportsFiltering
        {
            get
            {
                return true;
            }
        }

        // Nested Types
        public delegate void DeletingItemHandler(object sender, DeletingItemEventArgs e);

        public delegate void SavingItemHandler(object sender, SavingItemEventArgs e);
    }

}
